﻿using ServerBase.Config;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Timers;
using System.Windows.Threading;
using ServerBase.Client;
using ServerBase.Protocol;
using System.Windows.Controls;
using System.Dynamic;
using System.Windows.Data;
using UtilWpf;
using ServerBase.Other;
using UtilPublic.ApiDispatcher;
using UtilPublic.Serialization;
using System.Text;

namespace ToolGame
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application, IUtilLoger
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            Conf.InitConfTool();

            TimedInit();

            LunarClientDebug.SetLoger(this);

        }

        //主窗口
        public static MainWindow WdMain = null;
        public static PageDebug PgDebug = null;

        #region 定时器
        private Timer aTimer = null;
        private delegate void TimerDispatcherDelegate();
        private void TimedInit()
        {
            aTimer = new Timer(200);
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 200;
            aTimer.Enabled = true;
        }
        private void OnTimedEvent(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal,
                new TimerDispatcherDelegate(LunarClientDispatcher.OnDispatcher));
        }
        #endregion

        #region 公共
        public static string TxtDebug = "";
        public static void DebugInfo(object o)
        {
            MessageBox.Show($"{o}", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        public static void DebugError(object o)
        {
            MessageBox.Show($"{o}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
        }
        public static void DebugDebug(object o)
        {
            if (PgDebug == null) 
            {
                TxtDebug = $"{o}\r\n{TxtDebug}";
            }
            else
            {
                PgDebug.TextBoxDebug.Text = $"{o}\r\n{PgDebug.TextBoxDebug.Text}";
            }
        }
        public static void DebugMsg(object o)
        {
            var msg = (ProtocolMsgBase)o;
            var imsg = (ProtocolMsgBase)o;
            DebugDebug(msg.Result);
            DebugDebug(Jsh.Jstr(imsg));
        }
        public void Error(object o)
        {
            DebugError(o);
        }
        public void Debug(object o)
        {
            DebugDebug(o);
        }
        #endregion

        #region 综合
        public static string GmAccount = "pub";
        public static LunarClientLink ClientNetSingle = new LunarClientLink();
        public static B2G_Gt_Action GetReq(EGtAction GtAction, string GtArgs = "")
        {
            return new B2G_Gt_Action()
            {
                Shuttle = GmAccount,
                GtAction = $"{GtAction}",
                GtArgs = GtArgs,
            };
        }
        #endregion

        

        #region dataGrid绑定类
        //输出此类的所有属性名和属性对应的值
        public static List<UserControlColumnNode> GetUserControlColumnNodeList<T>() where T : new()
        {
            var result = new List<UserControlColumnNode>();
            var t = new T();
            var fields = t.GetType().GetFields();
            foreach (var item in fields)
            {
                var objs = item.GetCustomAttributes(typeof(DescAttribute), true);
                if (objs.Length > 0)
                {
                    if (item.FieldType.IsValueType || item.FieldType.Name.StartsWith("String"))
                    {
                        result.Add(new UserControlColumnNode(((DescAttribute)objs[0]).DescStr, item.Name));
                    }
                }
            }
            return result;
        }
        private static object lockDataGridShow = new object();
        public static void DataGridShowObj<T>(DataGrid dgv, T obj, int 表格列宽 = 200)
        {
            lock (lockDataGridShow)
            {
                dgv.Columns.Clear(); dgv.Items.Clear();
                dgv.Columns.Add(new DataGridTextColumn() { Header = "数据", Binding = new Binding("DataDesc"), Width = 表格列宽, });
                dgv.Columns.Add(new DataGridTextColumn() { Header = "数据项", Binding = new Binding("DataName"), Width = 表格列宽, });
                dgv.Columns.Add(new DataGridTextColumn() { Header = "数据值", Binding = new Binding("DataValue"), Width = 表格列宽, });

                BasePropMgr.GetFieldsDescList(obj, out var ListDatas);
                foreach (var item in ListDatas)
                {
                    dynamic ditem = new ExpandoObject();
                    ditem.DataDesc = item.Desc;
                    ditem.DataName = item.Label;
                    ditem.DataValue = item.Value;
                    dgv.Items.Add(ditem);
                }
            }
        }
        public static void DataGridShowDict(DataGrid dgv, Dictionary<string, string> dict, int 表格列宽 = 200)
        {
            lock (lockDataGridShow)
            {
                dgv.Columns.Clear(); dgv.Items.Clear();
                dgv.Columns.Add(new DataGridTextColumn() { Header = "Key", Binding = new Binding("Key"), Width = 表格列宽, });
                dgv.Columns.Add(new DataGridTextColumn() { Header = "Value", Binding = new Binding("Value"), Width = 表格列宽, });

                foreach (var kvp in dict)
                {
                    dynamic ditem = new ExpandoObject();
                    ditem.Key = kvp.Key;
                    ditem.Value = kvp.Value;
                    dgv.Items.Add(ditem);
                }
            }
        }
        #endregion

        
    }
}
